<?php
namespace app\admin\controller;
\think\Loader::import('controller/Jump', TRAIT_PATH, EXT);
use app\admin\Controller;
use think\Db;
use think\Lang;
use think\Loader;
use think\Exception;

class Videolist extends Controller
{
	use \app\admin\traits\controller\Controller;	
//	use \app\admim\traits\controller\Controller;
	use \traits\controller\Jump;
    /**
     * 列表
     */     
    public function index()
    {
    	$name = $_GET['name'];
    	if($name){
        	$map['name'] = ["like", "%" . $name . "%"];
        }
		$map['isdelete'] = 0;
		$model = $this->getModel('Videolist');
        $listRows = 10;
      // 分页查询
        $list = $model->where($map)->paginate($listRows, false, ['query' => $this->request->get()]);
        // 模板赋值显示
        $this->view->assign('list', $list);
        $this->view->assign("page", $list->render());
        $this->view->assign("count", $list->total());
        $this->view->assign('numPerPage', $listRows);
			
        return $this->view->fetch();
    }

	/**
     * 编辑
     * @return mixed
     */
    public function edit()
    {
        $controller = $this->request->controller();
        $module = $this->request->module();

        if ($this->request->isAjax()) {
            // 更新
            $data = $this->request->post();
            if (!$data['id']) {
                return ajax_return_adv_error("缺少参数ID");
            }
                       
            // 验证
            if (class_exists(Loader::parseClass($module, 'validate', $controller))) {
                $validate = Loader::validate($controller);
                if (!$validate->check($data)) {
                    return ajax_return_adv_error($validate->getError());
                }
            }
            // 更新数据
            //dump($data);exit;
            Db::startTrans();
            try {
                //$data['addtime'] = time();
                if($data['is_rz'] == 0){
                	$data['is_rz'] = 0;
                	$data['real_name'] = '';
                	$data['IDcard'] = '';
                	$data['card_photo2'] = '';
                }
                Db::table("jx_videolist")->where(" id = ".$data['id'])->update($data);
				
				
                // 提交事务
                Db::commit();
				
                return ajax_return_adv("编辑成功");
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
				//echo Db::table("jx_videolist")->getLastSql();die;
                return ajax_return_adv_error($e->getMessage());
            }
        } else {
            // 编辑
            $id = $rId = $this->request->param('id');
            if (!$id) {
                throw new Exception("缺少参数ID");
            }
            $vo = $this->getModel($controller)->find($rId);
			
            if (!$vo) {
                throw new HttpException(404, '该记录不存在');
            }
            $this->view->assign("vo", $vo);

            return $this->view->fetch();
        }
    }
	




}

